package com.gxm.base.thread.习题.基本;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 多线程的三种实现方式
 */
public class ThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadOne threadOne = new ThreadOne();
        ThreadOne threadOne1 = new ThreadOne();
        threadOne.start();
        threadOne1.start();

        Runnable runnable = new ThreadRun();
        Thread runThread = new Thread(runnable);
        runThread.run();
        runThread.start();
        //匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("匿名内部类写法");
            }
        }).start();
        //lambda写法
        new Thread(() -> {
            System.out.println("lambda写法");
        }).start();

        FutureTask<String> futureTask = new FutureTask<String>(new Callable01());
        new Thread(futureTask).start();
        //线程是否已取消
        futureTask.isCancelled();
        //线程取消
        //futureTask.cancel();
        System.out.println(futureTask.get());
    }
}
class ThreadOne extends Thread{
    @Override
    public void run(){
        System.out.println("ThreadOne run......");
    }
}

class ThreadRun implements Runnable{

    @Override
    public void run() {
        System.out.println("ThreadRun run.....");
    }
}

class Callable01 implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("Callable01 run......");
        return "true";
    }
}